package development;

import java.util.Vector;

import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.font.FontManager;
import state.font.ImageText;
import state.socket.DataPackage;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import development.bean.UserBean;
import development.group.FrmGroupMenu;
import development.listener.DownloadResourceListener;
import development.socket.ProtocolConstants;
import development.socket.SocketClientUtil;

public class FrmAutoInviteGame extends GameForm {
  private final int DISTANCE = 21;
  private final int DX = 10;
  private final int DY = 48;
  
  private GameImage checkedImage;
  private GameImage uncheckedImage;
  private GameImage popupImage;
  
  private int chooseIndex;
  public int pagenummber;
  public int totalPage;
  private int page;
  private boolean showListUser;
  private boolean showListGame;
  private String gameId;
  
  private ImageText text8;
  private ImageText text11;
  
  private boolean backFromDownloadResource = false;

  private static GameForm singleton;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmAutoInviteGame();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    page = 0;
    totalPage = 0;
    chooseIndex = 0;
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    text11 = FontManager.getFont(FontManager.FONT_SIZE_11);
    checkedImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "Tickbox_1.png");
    uncheckedImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "Tickbox_2.png");
    popupImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "bg_popup.png");
    
    SocketClientUtil.listInviteUser(page);
    DrawUtils.setBottomBar(_manager, null, "Chọn", "Trở về");
    _manager.enableTouchPad();
    
    if (backFromDownloadResource) {
      showListUser = true;
      showListGame = false;
      backFromDownloadResource = false;
    } else {
      showListUser = false;
      showListGame = true;
      gameId = "";
    }
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    if (showListUser) {
      drawFreeUsers(g);
    }
    if (showListGame) {
      drawGameList(g);
    }
  }

  private void drawGameList(Graphics g) {
    text11.setBold(true);
    text11.drawString2(g, "Chọn Game", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 5, GameConstants.TOP_HCENTER_ANCHOR);
    for (int i = 0; i < Constants.GAME_NAME.len(); i++) {
      g.setColor(GameColor.WHITE_CODE);
      g.drawLine(DX, DY - DISTANCE / 2 + i * DISTANCE, GameConstants.SCREEN_WIDTH - DX, DY - DISTANCE / 2 + i * DISTANCE);
      int color = GameColor.WHITE_CODE;
      if (i == chooseIndex) {
        g.setColor(0x662221);
        g.fillRect(DX, DY - DISTANCE / 2 + i * DISTANCE + 2, GameConstants.SCREEN_WIDTH - 2 * DX, DISTANCE - 3);
        color = GameColor.YELLOW_CODE;
      }
      text8.drawString2(g, Constants.GAME_NAME.get(i), color, 26, DY + i * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
    }
  }

  private void drawFreeUsers(Graphics g) {
    text11.setBold(true);
    text11.drawString2(g, "Chọn người chơi", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 5, GameConstants.TOP_HCENTER_ANCHOR);
    if ((Global.freeUser.size() == 0)) {
      g.drawImage(popupImage, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
      text8.drawString2(g, "Không có người chơi đang chờ", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
    } else {
      for (int i = 0; i < Global.freeUser.size(); i++) {
        g.setColor(GameColor.WHITE_CODE);
        g.drawLine(DX, DY - DISTANCE / 2 + i * DISTANCE, GameConstants.SCREEN_WIDTH - DX, DY - DISTANCE / 2 + i * DISTANCE);

        UserBean user = (UserBean) Global.freeUser.elementAt(i);
        int color = GameColor.WHITE_CODE;
        if (i == chooseIndex) {
          g.setColor(0x662221);
          g.fillRect(DX, DY - DISTANCE / 2 + i * DISTANCE + 2, GameConstants.SCREEN_WIDTH - 2 * DX, DISTANCE - 3);
          color = GameColor.YELLOW_CODE;

          if (user.isChoosed()) {
            DrawUtils.getBottomBar(_manager).setCenterLabel("Bỏ chọn");
          } else {
            DrawUtils.getBottomBar(_manager).setCenterLabel("Chọn");
          }
        }

        if (user.isChoosed()) {
          g.drawImage(checkedImage, 15, DY + i * DISTANCE, GameConstants.CENTER_ANCHOR);
        } else {
          g.drawImage(uncheckedImage, 15, DY + i * DISTANCE, GameConstants.CENTER_ANCHOR);
        }

        text8.drawString2(g, user.getUsername(), color, 26, DY + i * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
        text8.drawString2(g, user.getLocationInfo(), color, GameConstants.SCREEN_WIDTH / 2 - 30, DY + i * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
      }

      if (Global.freeUser.size() > 0) {
        text8.drawString2(g, "Trang: ", GameColor.GREEN_CODE, 10, GameConstants.SCREEN_HEIGHT - 35, GameConstants.BOTTOM_LEFT_ANCHOR);
        for (int i = 0; i < pagenummber; i++) {
          if (page == i) {
            text8.drawString2(g, String.valueOf(i + 1), GameColor.BLACK_CODE, 55 + i * 10, GameConstants.SCREEN_HEIGHT - 35, GameConstants.BOTTOM_LEFT_ANCHOR);
          } else {
            text8.drawString2(g, String.valueOf(i + 1), GameColor.GREEN_CODE, 55 + i * 10, GameConstants.SCREEN_HEIGHT - 35, GameConstants.BOTTOM_LEFT_ANCHOR);
          }
        }
        DrawUtils.getBottomBar(_manager).setLeftLabel("Chơi");
      }
    }
  }

  public void onRecieveData(DataPackage dataPackage) {
    int header = dataPackage.getHeader();
    switch (header) {
    case ProtocolConstants.ResponseHeader.AUTO_INVITE_LIST_USER_RESPONSE:
      Global.freeUser.removeAllElements();
      int listSize = dataPackage.nextInt();
      for (int i = 0; i < listSize; i++) {
        UserBean friend = new UserBean();
        friend.setUsername(dataPackage.nextString());
        friend.setLocationInfo(dataPackage.nextString());
        Global.freeUser.addElement(friend);
      }
      break;
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.UP:
    case Key.K_2:
      chooseIndex--;
      if (showListUser) {
        if (chooseIndex < 0) {
          chooseIndex = Global.freeUser.size() - 1;
        }
      } else if (showListGame) {
        if (chooseIndex < 0) {
          chooseIndex = Constants.GAME_NAME.len() - 1;
        }
      }
      break;
    case Key.DOWN:
    case Key.K_8:
      chooseIndex++;
      if (showListUser) {
        if (chooseIndex > Global.freeUser.size() - 1) {
          chooseIndex = 0;
        }
      } else if (showListGame) {
        if (chooseIndex > Constants.GAME_NAME.len() - 1) {
          chooseIndex = 0;
        }
      }
      break;
    case Key.LEFT:
    case Key.K_4:
      if (showListUser) {
        chooseIndex = 0;
        page--;
        if (page <= 0) {
          page = 0;
        }
        SocketClientUtil.listInviteUser(page);
      }
      break;
    case Key.RIGHT:
    case Key.K_6:
      if (showListUser) {
        chooseIndex = 0;
        page++;
        if (page >= pagenummber) {
          page = pagenummber - 1;
        }
        SocketClientUtil.listInviteUser(page);
      }
      break;
    case Key.FIRE:
    case Key.K_5:
      if (showListUser) {
        if (Global.freeUser.size() > 0) {
          UserBean user = (UserBean) Global.freeUser.elementAt(chooseIndex);
          user.setChoosed(!user.isChoosed());
        }
      } else if (showListGame) {
        gameId = Constants.GAME_ID.get(chooseIndex);
        if (gameId == null || gameId.equals("")) {
          Global.alert.showAlert(this, GameAlert.OK_TYPE, "Hãy chọn game");
        } else {
          if (Global.resourceConfig.get(gameId) != null) {
            backFromDownloadResource = true;
            DownloadResourceListener downloadResourceListener = new DownloadResourceListener(gameId);
            downloadResourceListener.nextForm = this;
            Global.authenticateHandle.addListener(downloadResourceListener);
            GameGlobal.nextState(FrmDownloadResource.instance(), null);
            return;
          }
          showListGame = false;
          showListUser = true;
        }
      }
      break;
    case Key.SOFT_RIGHT:
      GameGlobal.nextState(FrmGroupMenu.instance(), null);
      break;
    case Key.SOFT_LEFT:
      if (showListUser) {
        if (Global.freeUser.size() > 0) {
          Vector choosedUsers = new Vector();
          for (int i = 0; i < Global.freeUser.size(); i++) {
            UserBean choosedUser = (UserBean) Global.freeUser.elementAt(i);
            if (choosedUser.isChoosed()) {
              choosedUsers.addElement(choosedUser.getUsername());
            }
          }
          
          if(choosedUsers.size() == 0){
            Global.alert.showAlert(this, GameAlert.OK_TYPE, "Hãy chọn người chơi");
          } else {
            if (Global.freeUser.size() > 0) {
              SocketClientUtil.invitePlay(choosedUsers, gameId);
            }
          }
        } else {
          Global.alert.showAlert(this, GameAlert.OK_TYPE, "Hãy chọn người chơi");
        }
      }
      break;
    }
  }

  public void destroy() {
    text8 = null;
    text11 = null;
    checkedImage = null;
    uncheckedImage = null;
    popupImage = null;
  }
}
